#include <bits/stdc++.h>
using namespace std;

void work() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a.begin() + 1, a.begin() + n + 1, greater<int>());

  vector<int> ci(n + 1, 0); // 差分数组
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    ci[l]++;
    ci[r + 1]--;
  }
  for (int i = 1; i <= n; i++) {
    ci[i] = ci[i - 1] + ci[i]; // 前缀和运算
  }
  sort(ci.begin() + 1, ci.begin() + n + 1, greater<int>());

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (long long)a[i] * ci[i];
  }
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    work();
  }
  return 0;
}